home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / GNU-SMALLTALK.lha / Collection.st < prev    next >
Text File  |  1992-02-15  |  7KB  |  276 lines

  1. "======================================================================
  2. |
  3. |   Collection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         15 Feb 92      Switched the collection creating enumerators to use
  34. |              the copyEmpty message, so it can be overridden by
  35. |              subclasses (like SortedCollection) when copying empty
  36. |              involves more than just doing a new.
  37. |
  38. | sbb         16 Mar 91      Class creation now separate statement.
  39. |
  40. | sbb          7 Feb 91      Fixed detect: to return the value that detect:ifNone:
  41. |              returns.
  42. |
  43. | sbb         21 Sep 90      Fixed store: to be storeOn:
  44. |
  45. | sbyrne     25 Apr 89      created.
  46. |
  47. "
  48.  
  49. Object subclass: #Collection
  50.        instanceVariableNames: ''
  51.        classVariableNames: ''
  52.        poolDictionaries: ''
  53.        category: nil
  54. !
  55.  
  56. Collection comment: 
  57. 'I am an abstract class.  My instances are collections of objects.  My
  58. subclasses may place some restrictions or add some definitions to how
  59. the objects are stored or organized; I say nothing about this.  I merely
  60. provide some object creation and access routines for general collections
  61. of objects.' !
  62.  
  63.  
  64. !Collection class methodsFor: 'instance creation'!
  65.  
  66. with: anObject
  67.     ^self new add: anObject; yourself
  68. !
  69.  
  70. with: firstObject with: secondObject
  71.     ^self new add: firstObject; add: secondObject; yourself
  72. !
  73.  
  74. with: firstObject with: secondObject with: thirdObject
  75.     ^self new add: firstObject; add: secondObject; add: thirdObject; yourself
  76. !
  77.  
  78. with: firstObject with: secondObject with: thirdObject with: fourthObject
  79.     ^self new add: firstObject; add: secondObject; add: thirdObject;
  80.         add: fourthObject; yourself
  81. ! !
  82.  
  83.  
  84.  
  85. !Collection methodsFor: 'Adding to a collection'!
  86.  
  87. add: newObject
  88.     self subclassResponsibility
  89. !
  90.  
  91. addAll: aCollection
  92.     "Adds all the elements of 'aCollection' to the receiver"
  93.     aCollection do: [ :element | self add: element ].
  94.     ^aCollection
  95. ! !
  96.  
  97.  
  98.  
  99. !Collection methodsFor: 'Removing from a collection'!
  100.  
  101. remove: oldObject ifAbsent: anExceptionBlock
  102.     self subclassResponsibility
  103. !
  104.  
  105. remove: oldObject
  106.     self remove: oldObject
  107.          ifAbsent: [ ^self error: 'Failed to remove object' ].
  108.     ^oldObject
  109. !
  110.  
  111. removeAll: aCollection
  112.     " ??? we're supposed to report an error if an object can't be removed
  113.       properly.  I've elected to let remove: report the error.  Also, it's
  114.       not clear whether we're supposed to remove all occurrences of the
  115.       members of aCollection from the receiver, or only the first."
  116.     aCollection do: [ :element | self remove: element ].
  117.     ^aCollection
  118. !!
  119.  
  120.  
  121.  
  122. !Collection methodsFor: 'testing collections'!
  123.  
  124. includes: anObject
  125.     self do: [ :element | anObject = element ifTrue: [ ^true ] ].
  126.     ^false
  127. !
  128.  
  129. isEmpty
  130.     ^self size == 0
  131. !
  132.  
  133. occurrencesOf: anObject
  134.     | count |
  135.     count _ 0.
  136.     self do: [ :element | anObject == element ifTrue: [ count _ count + 1 ] ].
  137.     ^count
  138. !
  139.  
  140. size
  141.     | count |
  142.     count _ 0.
  143.     self do: [ :element | count _ count + 1].
  144.     ^count
  145. !!
  146.  
  147.  
  148.  
  149. !Collection methodsFor: 'enumerating the elements of a collection'!
  150.  
  151. do: aBlock
  152.     self subclassResponsibility
  153. !
  154.  
  155. select: aBlock
  156.     | newCollection |
  157.     newCollection _ self copyEmpty.
  158.     self do: [ :element | (aBlock value: element)
  159.                             ifTrue: [ newCollection add: element ]
  160.          ].
  161.     ^newCollection
  162. !
  163.  
  164. reject: aBlock
  165.     | newCollection |
  166.     newCollection _ self copyEmpty.
  167.     self do: [ :element | (aBlock value: element)
  168.                             ifFalse: [ newCollection add: element ]
  169.          ].
  170.     ^newCollection
  171. !
  172.  
  173. collect: aBlock
  174.     | newCollection |
  175.     newCollection _ self copyEmpty.
  176.     self do: [ :element | newCollection add: (aBlock value: element) ].
  177.     ^newCollection
  178. !
  179.  
  180. detect: aBlock ifNone: exceptionBlock
  181.     self do: [ :element | (aBlock value: element) ifTrue: [ ^element ] ].
  182.     exceptionBlock value    
  183. !
  184.     
  185. detect: aBlock
  186.     ^self detect: aBlock ifNone: [ self error: 'Collection detect: failed']
  187. !
  188.  
  189. inject: thisValue into: binaryBlock
  190.     self do: [ :element | thisValue _ binaryBlock value: thisValue
  191.                                                   value: element ].
  192.     ^thisValue
  193. !!
  194.  
  195.  
  196.  
  197. !Collection methodsFor: 'converting'!
  198.  
  199. asBag
  200.     | aBag |
  201.     aBag _ Bag new.
  202.     self do: [ :element | aBag add: element ].
  203.     ^aBag
  204. !
  205.  
  206. asSet
  207.     | aSet |
  208.     aSet _ Set new: self size.
  209.     self do: [ :element | aSet add: element ].
  210.     ^aSet
  211. !
  212.  
  213. asOrderedCollection
  214.     | anOrderedCollection |
  215.     anOrderedCollection _ OrderedCollection new: self size.
  216.     self do: [ :element | anOrderedCollection add: element ].
  217.     ^anOrderedCollection
  218. !
  219.  
  220. asSortedCollection
  221.     | aSortedCollection |
  222.     aSortedCollection _ SortedCollection new.
  223.     self do: [ :element | aSortedCollection add: element ].
  224.     ^aSortedCollection
  225. !
  226.  
  227. asSortedCollection: aBlock
  228.     | aSortedCollection |
  229.     aSortedCollection _ SortedCollection sortBlock: aBlock.
  230.     self do: [ :element | aSortedCollection add: element ].
  231.     ^aSortedCollection
  232. ! !
  233.  
  234.  
  235.  
  236. !Collection methodsFor: 'copying'!
  237.  
  238. copyEmpty
  239.     ^self species new
  240. ! !
  241.     
  242.  
  243.  
  244. !Collection methodsFor: 'printing'!
  245.  
  246. printOn: aStream
  247.     aStream nextPutAll: self classNameString.
  248.     aStream nextPutAll: ' ('.
  249.     self do:
  250.         [ :element | element printOn: aStream.
  251.              aStream space ].
  252.     aStream nextPut: $)
  253. !!
  254.  
  255.  
  256.  
  257. !Collection methodsFor: 'storing'!
  258.  
  259. storeOn: aStream
  260.     | noElements |
  261.     aStream nextPut: $(.
  262.     aStream nextPutAll: self classNameString.
  263.     aStream nextPutAll: ' new'.
  264.     noElements _ true.
  265.     self do:
  266.         [ :element | aStream nextPutAll: ' add: '.
  267.                      element storeOn: aStream.
  268.              aStream nextPut: $;.
  269.              noElements _ false ].
  270.     noElements ifFalse: [ aStream nextPutAll: ' yourself' ].
  271.     aStream nextPut: $)
  272. !!
  273.  
  274.